package com.hzx.controller;

import com.hzx.pojo.Order;
import com.hzx.service.OrderService;
import com.hzx.until.FileUpload;
import com.hzx.until.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class ShopDemandController {

    private String ORDERPICTUREPATH = "C:\\Users\\49975\\Desktop\\数据库\\DBPJ\\spring02\\src\\main\\resources\\static\\img\\OrderPicture\\";
    //private static String ORDERPICTUREPATH = "F:\\newideafile\\dbpj\\src\\main\\resources\\static\\img\\OrderPicture\\";
    @Autowired
    private OrderService orderService ;

    @GetMapping("/shopdemand.html")
    public  String shopDemandController() {
        return "shopdemand";
    }

    @RequestMapping(value = "/shopdemand",method = RequestMethod.POST)
    public String shopDemand(HttpServletRequest request,
                             Model model ,
                             @RequestParam String name ,
                             @RequestParam String content ,
                             @RequestParam String price ,
                             @RequestParam String take_way ,
                             @RequestParam Integer kid ,
                             @RequestParam("img") MultipartFile file) {

        Integer uid = (Integer) request.getSession().getAttribute("uid") ;
        if(StringUntil.isNullOrEmpty(uid)||uid==0){
            return "index" ;
        }
        Order order = new Order();
        order.setUid(uid);

        if (!StringUntil.isNullOrEmpty(name)) {
            order.setFind_name(name);
        }
        if (!StringUntil.isNullOrEmpty(kid)) {
            order.setKid(kid);
        }
        if (!StringUntil.isNullOrEmpty(content)) {
            order.setContent(content);
        }

        if (!StringUntil.isNullOrEmpty(price)) {
            order.setPrice(Double.parseDouble(price));
        }

        if (!StringUntil.isNullOrEmpty(take_way)) {
            order.setTake_way(take_way);
        }

        order.setBtime(new Date(System.currentTimeMillis()));
        order.setEtime(new Date(System.currentTimeMillis()+1000*3600*24*30));
        int ans = orderService.insertBySelective(order);

        Integer id = orderService.selectIdBySelective(order) ;

        FileUpload.File_upload(file,ORDERPICTUREPATH,id+".jpg");

        if (ans != 1) {
            return "error";
        } else {
            model.addAttribute(order);
            return "shopdemand";
        }
    }

    // 删除求购信息
    @GetMapping("/deletedemand/{id}")
    public String deleteDemand(@PathVariable("id") Integer id) {
        Integer ans = orderService.deleteById(id);
        if(ans!=1) {
            return "error" ;
        }
        return "redirect:/personal-center";
    }

    @GetMapping("/setOrderStatus/{id}")
    public String setOrderStatus(@PathVariable("id") Integer id) { // 传入该商品的 id

        orderService.updateOrderStatus(id); // 通过求购的id 修改求购的 状态  默认 1 为完成求购  null 未完成

        return "redirect:/personal-center";
    }

}
